package com.pd.modules.server.dialogs;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

import com.pd.modules.server.ConnectedClientInfo;
import com.pd.modules.server.ConnectedClients;
import com.pd.modules.server.Server;

@SuppressWarnings("serial")
public class DialogListConnections extends JFrame implements Observer
{

	private JPanel contentPane;
	private JTable mTable;

	private String[] mTableColumns = new String[] { "Thread ID", "Username", "IP" };
	private DefaultTableModel mTableModel = null;
	private JMenuBar menuBar;
	private JMenu mnFicheiro;
	private JMenuItem mMenuItemDisconnect;
	private JMenuItem mntmSair;

	private void showAllClients()
	{
		ConnectedClientInfo[] clients = ConnectedClients.getInstance().getAllClients();

		Object[][] rows = new Object[clients.length][2];
		for (int i = 0; i < clients.length; i++)
		{
			rows[i] = clients[i].getAsTableRow();
		}

		refreshTable(rows);
	}

	private void refreshTable(Object[][] _data)
	{
		mTableModel = new DefaultTableModel(_data, mTableColumns);
		mTable.setModel(mTableModel);

		// Alinha o texto ao centro
		DefaultTableCellRenderer dtcr = new DefaultTableCellRenderer();
		dtcr.setHorizontalAlignment(SwingConstants.CENTER);
		for (String columnName : mTableColumns)
			mTable.getColumn(columnName).setCellRenderer(dtcr);

		mMenuItemDisconnect.setEnabled(false);

	}

	/**
	 * Create the frame.
	 */
	public DialogListConnections()
	{
		setTitle("Clientes actualmente ligados...");
		setResizable(false);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 300);

		ConnectedClients.getInstance().addObserver(this);

		menuBar = new JMenuBar();
		setJMenuBar(menuBar);

		mnFicheiro = new JMenu("Ficheiro");
		menuBar.add(mnFicheiro);

		mMenuItemDisconnect = new JMenuItem("Desconectar cliente...");
		mMenuItemDisconnect.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				int idx = mTable.getSelectedRow();
				long threadID = (long) mTable.getModel().getValueAt(idx, 0);

				Server.getInstance().disconnectClientThread(threadID);
			}
		});
		mnFicheiro.add(mMenuItemDisconnect);

		mntmSair = new JMenuItem("Sair");
		mntmSair.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				setVisible(false);
			}
		});
		mnFicheiro.add(mntmSair);

		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));

		JScrollPane scrollPane = new JScrollPane();
		contentPane.add(scrollPane);

		mTable = new JTable()
		{
			@Override
			public boolean isCellEditable(int rowIndex, int colIndex)
			{
				return false; // Disallow the editing of any cell
			}
		};
		mTable.setModel(new DefaultTableModel(null, mTableColumns));
		mTable.getSelectionModel().addListSelectionListener(new EventSelectionChanged());
		mTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		scrollPane.setViewportView(mTable);

		showAllClients();
		scrollPane.setViewportView(mTable);
	}

	@Override
	public void update(Observable arg0, Object arg1)
	{
		showAllClients();
	}

	class EventSelectionChanged implements ListSelectionListener
	{

		@Override
		public void valueChanged(ListSelectionEvent e)
		{
			mMenuItemDisconnect.setEnabled(true);
		}
	}

}
